﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
using Nivdal.TBP;

namespace Nivdal
{
    public partial class MailMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.MailMessage_PageTitle;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            

            string MailId = null;
            if (Request.QueryString["MailId"] != null)
                MailId = Request.QueryString["MailId"].ToString();

            if ((MailId != null) && (GND.ReadNivdalBoolData("UserId", "MailId", "Nivdal_MailCanUserSeeMail", ou.UserId, MailId)))
            {
                if (!IsPostBack)
                {
                    this.GetMailSqlDataSource.SelectParameters.Add("MailId", MailId);
                    this.GetMailSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                }

                (FormView1.FindControl("HistoryLabel") as Label).Text = MailBody((GND.ReadNivdalStringData("@MailId", "Language", "Nivdal_MailGetMailHistoryWithoutCurent", MailId, ou.Language)));

                MessageIdHiddenField.Value = MailId;
                //string PreText = null;
                bool IsMailMarkAsRead = false;
                string ToId = null;
                ToId = (GND.ReadNivdalStringData("@MailId", "Nivdal_MailGetToId", MailId));

                if ((ToId == null))
                    Response.Redirect("~/Default.aspx");
                if (LoggedInIsReciver(ToId))
                {
                    IsMailMarkAsRead = (GND.ReadNivdalBoolData("@MailId", "Nivdal_MailMarkMailAsRead", MailId));
                }
            }
            else
                Response.Redirect("~/Default.aspx");

        }

        protected void SubmitSpamReport(object sender, EventArgs e)
        { 
            string note = (FormView1.FindControl("NopeTextArea") as CustomWebControls.TextArea).Text;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("MailId", "Note", "Language", "Nivdal_MarkMailAsSpamByReceiver", MessageIdHiddenField.Value, note, ou.Language);
            MessageLabel.Text = Answer;
            MessagePanel.Visible = true;

            FormView1.DataBind();

        }

        protected void CancelSpamReport(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string Answer = GND.ReadNivdalStringData("MailId", "Nivdal_UnMarkMailAsSpamByReceiver", MessageIdHiddenField.Value);
            MessageLabel.Text = Answer;
            MessagePanel.Visible = true;
            FormView1.DataBind();                
        }

        protected string MailBody(Object MB)
        {
            string MBSrc = MB.ToString();
            return GlobalFunctions.EncodeHtmlFromText(MBSrc);
           
        }

        protected bool LoggedInIsReciver(Object obj1)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            return (obj1.ToString() == ou.UserId);
        }

        protected bool LoggedInIsReciver(string ToId)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string UsrId = ou.UserId.ToUpper();
            return (ToId == UsrId);
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void DeleteMessage(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            GND.ExecNivdalSQL("MailId", "UserId", "Nivdal_MailDeleteMail", MessageIdHiddenField.Value, ou.UserId);
            Response.Redirect("MailInbox.aspx?ConfirmMessage=" + Resources.Resource.MailMessage_DeleteMessageApprovalText);
        }

        protected string ReturnConvertedForumString(Object Input)
        {
            string InputString = Input.ToString();

            TBP.ForumConvert FC = new ForumConvert();
            return FC.ForumConvertString(InputString);

        }
    }

    
}